/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 - 2008 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/

package com.genia.toolbox.web.servlet;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;

import org.apache.cxf.bus.spring.SpringBusFactory;
import org.apache.cxf.transport.servlet.CXFServlet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import com.genia.toolbox.spring.provider.init.bean.SpringInitializable;

/**
 * Custom {@link CXFServlet} that use spring to retrieve the application
 * context.
 */
public class CustomCXFServlet
    extends CXFServlet
    implements ApplicationContextAware, SpringInitializable
{

  /**
   * the <code>Log</code> variabe.
   */
  private static final Logger LOGGER = LoggerFactory.getLogger(CustomCXFServlet.class);

  /**
   * the {@link ServletConfig} of the servlet.
   */
  private ServletConfig servletConfig;



  /**
   * initialization of the servlet.
   * 
   * @param servletConfig
   *          the configuration of the servlet.
   */
  @Override
  public void init(ServletConfig servletConfig)
  {
    this.servletConfig = servletConfig;
  }



  /**
   * real initialization of the servlet. This will be done only once the
   * {@link ApplicationContext} is completly initialized.
   */
  @Override
  public void init()
  {
    ServletConfig servletConfig = this.servletConfig;
    this.servletConfig = null;
    if (servletConfig != null) {
      try {
        super.init(servletConfig);
      }
      catch (ServletException e) {
        LOGGER.error(e.getMessage(), e);
      }
    }
  }

  /**
   * the serial uid.
   */
  private static final long serialVersionUID = -8433834434236810892L;

  /**
   * the {@link ApplicationContext} to use.
   */
  private ApplicationContext applicationContext;



  /**
   * load the {@link org.apache.cxf.Bus} to use.
   * 
   * @param servletConfig
   *          the {@link ServletConfig} of the servlet
   * @throws ServletException
   *           if an error occured
   */
  @Override
  public void loadBus(ServletConfig servletConfig)
      throws ServletException
  {
    bus = new SpringBusFactory(getApplicationContext()).createBus();
    replaceDestinationFactory();
    controller = createServletController(servletConfig);
  }



  /**
   * getter for the applicationContext property.
   * 
   * @return the applicationContext
   */
  public ApplicationContext getApplicationContext()
  {
    return applicationContext;
  }



  /**
   * setter for the applicationContext property.
   * 
   * @param applicationContext
   *          the applicationContext to set
   */
  public void setApplicationContext(ApplicationContext applicationContext)
  {
    this.applicationContext = applicationContext;
  }



  /**
   * getter for the order property. This property allow to sort the
   * {@link SpringInitializable}.
   * 
   * @return the order
   * @see com.genia.toolbox.spring.provider.init.bean.SpringInitializable#getOrder()
   */
  public int getOrder()
  {
    return 0;
  }

}
